#!/usr/bin/perl

# $Id: 19_buildstepfactory.t 278 2010-07-18 13:38:38Z dolbanavt $
# $Date: 2010-07-18 16:38:59 +0300 (Вск, 18 Июл 2010) $
# $HeadURL: https://robosearch.googlecode.com/svn/branches/reborn/t/19_buildstepfactory.t $
# $LastChangedRevision$

use FindBin qw( $RealBin );
use File::Spec::Functions qw( catfile );

BEGIN {
    push @INC, catfile( $RealBin, '..', 'base_lib' );
    push @INC, catfile( $RealBin, '..', 'lib' );
}

use JIP::ToolSet;
use English qw( -no_match_vars );
use Test::More 0.88;
use Const::Fast qw( const );
use Test::MockObject;
use Test::Exception;

const my $MODULE => 'BuildStepFactory';

const my $SINGLETON_REQUESTED => 0;

use_ok $MODULE;
require_ok $MODULE;

can_ok $MODULE, qw(
    create _instantiate_class
);

throws_ok { $MODULE->create(); }
    qr{^step_name\srequired}xms,
    'create( step_name )';

throws_ok { $MODULE->create('step_name'); }
    qr{step_params\srequired}xms,
    'create( build_params )';

{
    my $mock = Test::MockObject->new();

    $mock->fake_module( 'FactoryMethod',
        _instantiate_class => sub {
            my $self = shift;

            is $self, 'FactoryMethod', 'fake FactoryMethod class';

            my %param = @ARG;

            is $param{'class_name'},
               'BuildCommand::SetAgeing',
               'mocked _instantiate_class(class_name)';

            is $param{'singleton_requested'},
               $SINGLETON_REQUESTED,
               'mocked _instantiate_class(singleton_requested)';

            my %arguments = @{ $param{'constructor_arguments'} };

            is $arguments{'conf'},
               'conf_object',
               'BuildCommand::SetAgeing( conf )';

            is $arguments{'log'},
               'logger',
               'BuildCommand::SetAgeing( log )';

            is $arguments{'dbh'},
               'database_wrapper',
               'BuildCommand::SetAgeing( dbh )';

            return bless {}, 'BuildCommand::SetAgeing';
        },
    );

    my $w = $MODULE->create( 'SetAgeing', {
        conf => 'conf_object',
        log  => 'logger',
        dbh  => 'database_wrapper',
    } );

    is ref $w,
       'BuildCommand::SetAgeing',
       'got BuildCommand::SetAgeing object';
}

done_testing();
